#-*- coding:UTF-8 -*-

import mmap,os,md5,struct
import dictzip
import zlib
import sys 
import time
import pickle
import StringIO
import HTTPUtility

class StarDict(object):
    def __init__(self,dictname):
        self.name=dictname
        self.data = None
        self.inx=None
        self.wordlist=[]
        self.initialize(dictname)
    
    def initialize(self,name):
        #self.data=open(name+".dict","rb")
        self.data=dictzip.DictzipFile(name+".dict.dz")
        self.inx=open(name+".idx",'rb').read()
        self.info=open(name+".ifo",'rb').read()
        runner=0
        self.wordlist.append(runner)
        i=0
        while True:
            runner = self.inx.find('\0',runner)
            if runner == -1:
                break
            runner+=9
            self.wordlist.append(runner)
            i+=1
    def get_word(self,index):
        "get word from self.index"
        left_b = self.wordlist[index]
        right_b = self.wordlist[index+1] - 9
        #print self.index[left_b:right_b]
        return self.inx[left_b:right_b]
    def get_explanation(self,index):
        "get word from self.index"
        right_b = self.wordlist[index+1] - 9
        offset_v = 0
        size_v =  0
        offset = self.inx[right_b+1:right_b+5]
        size = self.inx[right_b+5:right_b+9]
        offset_v = struct.unpack('!i',offset)[0]
        size_v = struct.unpack('!i',size)[0]

        self.data.seek(offset_v)
        return self.data.read(size_v)
    
    def __del__(self):
        if self.data:
            self.data.close()


def ELFHash(p):
    hash = 0
    x =0
    for s in p:
        hash = (hash<<4) + ord(s)
        x = hash & 0xF0000000L
        if x != 0:
            hash ^= (x >> 24);
            hash &= ~x;
    return (hash & 0x7FFFFFFF)

if __name__ == "__main__":
    BUCKSIZE = 217
    dictname = r"D:\stardict-powerword2007_pwdecmc-2.4.2\powerword2007_pwdecmc"
    sdict = StarDict(dictname)
    sdictsize = len(sdict.wordlist) - 1

    d = [ {} for i in range(BUCKSIZE) ]
    for i in range(0, sdictsize):
        w = sdict.get_word(i).lower()
        h = abs(ELFHash(w)) % BUCKSIZE
        d[h][w] = sdict.get_explanation(i)
    for i in range(0, BUCKSIZE):
        print len(d[i])
        file = open("%d.dat"%i,"wb")
        pickle.dump(d[i], file, 2)
        file.close()
    print "Split Dict OK!"
    
    if True:
        host = "linxh.appspot.com"
        for i in range(0, BUCKSIZE):
            data = open("%d.dat"%i,"rb").read()
            resp = HTTPUtility.post_multipart(host,"/dict/uploaddata",
                                              [("dictid",str(i)),("dictname","pwdecmc")],
                                              [("data","data",data)])
            print i," ",resp.status," ",resp.read()
    
        